<template>
  <!-- 登录页面静态搭建 -->
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="loginForms"
        >
          <h1>Hello</h1>
          <h2>欢迎来到硅谷甄选</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              v-model="loginForm.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loading"
              class="loginButton"
              type="primary"
              @click="login"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElNotification } from 'element-plus'
//引入用户登录的小仓库
import useUserStore from '@/store/modules/user'
//引入判断登录时间工具函数
import { getHours } from '@/utils/time'
let useStore = useUserStore()
let $router = useRouter()
//获取el-Form组件身上的方法
let loginForms = ref()
//收集账号和密码的信息
let loginForm = reactive({ username: '', password: '' })
//按钮加载效果
let loading = ref(false)
//登录按钮回调
const login = async () => {
  //保证表单全部校验通过后发送登录请求
  await loginForms.value.validate()
  //开始加载效果
  loading.value = true
  //点击登录按钮以后干什么?
  //通知仓库发登录请求
  //请求成功后->首页展示数据的地方
  //请求失败->弹出登录失败信息
  // console.log("登录信息",loginForm)
  //可以用.then (()=>{})方写
  try {
    await useStore.userLogin(loginForm)
    //登录失败加载效果消失
    loading.value = false
    //登录成功后跳转到home页
    $router.push({
      path: '/',
    })
    //登录成功的提示
    ElNotification({
      type: 'success',
      message: '欢迎回来',
      title: `Hi,${getHours()}好`,
    })
  } catch (error: any) {
    //登录失败加载效果消失
    loading.value = false
    //登录失败的提示
    ElNotification({
      type: 'error',
      message: (error as Error).message,
    })
  }
}
// //定义表单检验需要配置对象
// const rules = {
//   //规则对象属性:
//   //required代表这个字段必须要校验的
//   //min:文本长度至少多少位
//   //max:文本长度最多多少位
//   //message:错误的提示信息
//   //trigger:触发校验表单的时机,change->文本发生变化的时候触发校验,blur:失去焦点的时候触发校验规则
//   username: [
//     // { required: true, message: '用户名不能为空', trigger: 'blur' },
//     {
//       required: true,
//       min: 5,
//       max: 10,
//       message: '账号长度至少五位',
//       trigger: 'change',
//     },
//   ],
//   password: [
//     // { required: true, message: '密码不能为空', trigger: 'blur' },
//     {
//       required: true,
//       min: 6,
//       max: 10,
//       message: '密码长度至少六位',
//       trigger: 'change',
//     },
//   ],
// }
//自定义表单验证规则函数
const validateUserName = (rules: any, value: any, callback: any) => {
  //rules:表单验证规则对象
  //value:表元素文本内容
  //callback回调函数,如果符合条件callBack放行即可
  //如果不符合条件callBack方法,注入错误提示信息
  if (value.length >= 5) {
    callback()
  } else {
    callback('输入的账号长度至少五位')
  }
}
const validatePasseord = (rules: any, value: any, callback: any) => {
  if (value.length >= 6) {
    callback()
  } else {
    callback('输入的密码长度至少五位')
  }
}
// 自定义表单检验需要配置对象
const rules = {
  username: [
    {
      required: true,
      validator: validateUserName,
      trigger: 'change',
    },
  ],
  password: [
    {
      required: true,
      validator: validatePasseord,
      trigger: 'change',
    },
  ],
}
</script>

<style lang="scss" scoped>
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;
  .login_form {
    position: absolute;
    width: 40%;
    top: 30vh;
    padding: 40px;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    h1 {
      font-size: 40px;
      color: white;
    }
    h2 {
      margin: 20px 0 20px 0;
      font-size: 20px;
      color: white;
    }
    .loginButton {
      width: 100%;
    }
  }
}
</style>
